#include <iostream>
#include <unistd.h>
#include <string>
//#include <thread>
using namespace std;

#include "Thread.hpp"
using namespace ThreadModule;

void threadrun(int& data)
{
    int num=data;
    while(num)
    {
        cout<<"num="<<num<<endl;
        num--;
    }
}
int main()
{
   Thread<int> t1(threadrun,10);
    t1.Start();

    t1.Join();
    return 0;
}



/* c++11 线程库
void threadrun(int num)
{
    while (true)
    {
        sleep(1);
        cout << "I am a thread,num=" << num << endl;
    }
}
int main()
{
    thread t1(threadrun, 10);
    thread t2(threadrun, 10);
    thread t3(threadrun, 10);
    thread t4(threadrun, 10);
    thread t5(threadrun, 10);
    while (true)
    {
        cout << "I am main thread" << endl;
        sleep(1);
    }
    t1.join();
    t2.join();
    t3.join();
    t4.join();
    t5.join();
    return 0;
}
*/

//线程独立栈结构验证(局部变量cnt地址不同)
/*  
void* threadrun(void* args)
{
    string threadname=static_cast<const char*>(args);
    int cnt=5;
    while(true)
    {
        printf("%s,pid=%d,cnt=%d,&cnt=%p\n",threadname.c_str(),getpid(),cnt,&cnt);
    }
    return nullptr;
}
int main()
{
    pthread_t tid1, tid2;
    pthread_create(&tid1, nullptr, threadrun, (void *)"thread -1");
    pthread_create(&tid2, nullptr, threadrun, (void *)"thread -2");

    pthread_join(tid1,nullptr);
    pthread_join(tid2,nullptr);

    return 0;
}

*/


//线程的局部存储
/*
__thread int g_val = 100;

void *threadrun1(void *args)
{
    string threadname = (const char *)(args);
    while (true)
    {
        sleep(1);
        printf("%s,g_val:%d,&g_val:%p\n", threadname.c_str(), g_val, &g_val);
        g_val--;
    }
}
void *threadrun2(void *args)
{
    string threadname = (const char *)(args);
    while (true)
    {
        sleep(1);
        printf("%s,g_val:%d,&g_val:%p\n", threadname.c_str(), g_val, &g_val);
    }
}
int main()
{
    pthread_t tid1,tid2;
    pthread_create(&tid1, nullptr, threadrun1, (void *)"thread -1");
    pthread_create(&tid2, nullptr, threadrun2, (void *)"thread -2");
    pthread_join(tid1,nullptr);
    pthread_join(tid2,nullptr);
    return 0;
}
*/